package com.example.demo.util;

import com.example.demo.pojo.dto.NodeAlgDTO;

import java.util.*;

/**
 * @author vaintale
 * @date 2025/6/30
 */
public class NodeAlgDataUtil {

    /**
     * @param pairs
     * @return {@link Map }<{@link String }, {@link String }>
     * return 1 => 3,5,90
     */
    public static Map<String, String> processTimeRangesWithMerge_Old(NodeAlgDTO[] pairs) {
        Map<String, Set<String>> tempMap = new HashMap<>();

        for (NodeAlgDTO pair : pairs) {
            // 分割时间范围
            String[] timeRange = pair.getTime().split("-");
            if (timeRange.length != 2) continue;

            try {
                int start = Integer.parseInt(timeRange[0]);
                int end = Integer.parseInt(timeRange[1]);

                // 确保范围有效
                if (start > end) continue;

                // 分割value值
                String[] values = pair.getValue().split(",");

                // 为范围内的每个数字添加所有value值
                for (int i = start; i <= end; i++) {
                    String key = String.valueOf(i);
                    Set<String> valueSet = tempMap.computeIfAbsent(key, k -> new LinkedHashSet<>());
                    // 保持插入顺序的Set
                    Collections.addAll(valueSet, values);
                }
            } catch (NumberFormatException e) {
                // 忽略格式错误的条目
                continue;
            }
        }

        // 转换Set为逗号分隔的字符串
        Map<String, String> resultMap = new TreeMap<>(Comparator.comparingInt(Integer::parseInt));
        tempMap.forEach((key, valueSet) -> {
            resultMap.put(key, String.join(",", valueSet));
        });

        return resultMap;
    }

    public static List<NodeAlgDTO> processTimeRangesWithMerge(NodeAlgDTO[] pairs) {
        Map<String, Set<String>> tempMap = new HashMap<>();

        for (NodeAlgDTO pair : pairs) {
            // 分割时间范围
            String[] timeRange = pair.getTime().split("-");
            if (timeRange.length != 2) continue;

            try {
                int start = Integer.parseInt(timeRange[0]);
                int end = Integer.parseInt(timeRange[1]);

                // 确保范围有效
                if (start > end) continue;

                // 分割value值
                String[] values = pair.getValue().split(",");

                // 为范围内的每个数字添加所有value值
                for (int i = start; i <= end; i++) {
                    String key = String.valueOf(i);
                    Set<String> valueSet = tempMap.computeIfAbsent(key, k -> new LinkedHashSet<>());
                    // 保持插入顺序的Set
                    Collections.addAll(valueSet, values);
                }
            } catch (NumberFormatException e) {
                // 忽略格式错误的条目
                continue;
            }
        }

        // 转换Set为逗号分隔的字符串并构建NodeAlgDTO列表
        // 使用TreeMap保持key的自然排序（数字升序）
        Map<String, Set<String>> sortedMap = new TreeMap<>(Comparator.comparingInt(Integer::parseInt));
        sortedMap.putAll(tempMap);

        List<NodeAlgDTO> result = new ArrayList<>();
        sortedMap.forEach((key, valueSet) -> {
            String value = String.join(",", valueSet);
            result.add(new NodeAlgDTO(key, value));
        });

        return result;
    }

    public static List<NodeAlgDTO> processTimeRangesWithMerge123(NodeAlgDTO[] pairs) {
        Map<String, Set<String>> tempMap = new HashMap<>();

        for (NodeAlgDTO pair : pairs) {
            // 分割时间范围
            String[] timeRange = pair.getTime().split("-");
            if (timeRange.length != 2) continue;

            try {
                int start = Integer.parseInt(timeRange[0]);
                int end = Integer.parseInt(timeRange[1]);

                // 确保范围有效
                if (start > end) continue;

                // 分割value值
                String[] values = pair.getValue().split(",");

                // 为范围内的每个数字添加所有value值
                for (int i = start; i <= end; i++) {
                    String key = String.valueOf(i);
                    Set<String> valueSet = tempMap.computeIfAbsent(key, k -> new LinkedHashSet<>());
                    // 保持插入顺序的Set
                    Collections.addAll(valueSet, values);
                }
            } catch (NumberFormatException e) {
                // 忽略格式错误的条目
                continue;
            }
        }

        // 转换Set为逗号分隔的字符串并构建NodeAlgDTO列表
        List<NodeAlgDTO> result = new ArrayList<>();
        tempMap.forEach((key, valueSet) -> {
            String value = String.join(",", valueSet);
            result.add(new NodeAlgDTO(key, value));
        });

        return result;
    }

    public static List<List<NodeAlgDTO>> createGroupedTimeValueList(NodeAlgDTO[] pairs) {
        // 先创建按时间分组的Map
        Map<String, List<NodeAlgDTO>> timeGroupedMap = new TreeMap<>(Comparator.comparingInt(Integer::parseInt));

        for (NodeAlgDTO pair : pairs) {
            String[] range = pair.getTime().split("-");
            if (range.length != 2) continue;

            try {
                int start = Integer.parseInt(range[0]);
                int end = Integer.parseInt(range[1]);

                if (start > end) continue;

                String[] values = pair.getValue().split(",");

                // 为范围内的每个时间点创建条目
                for (int time = start; time <= end; time++) {
                    String timeKey = String.valueOf(time);
                    List<NodeAlgDTO> entries = timeGroupedMap.computeIfAbsent(timeKey,
                            k -> new ArrayList<>());

                    for (String value : values) {
                        entries.add(new NodeAlgDTO(timeKey, value.trim()));
                    }
                }
            } catch (NumberFormatException e) {
                continue;
            }
        }
        
        // 将Map的值转换为List的List
        return new ArrayList<>(timeGroupedMap.values());
    }

}
